<?php
/*
 * Created on 2013-9-17
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
set_time_limit(0);

include_once("../configure/configure.php");		//--> global var
include_once("../lib/impl/CategoryImpl.php");
include_once("utils/FileUtils.php");
include_once("utils/StringUtils.php");

$fileUtils	= new FileUtils();		//--> Files







$oCatImpl	= new CategoryImpl(DB_TAG_CC);

//file format: "THISURL","CLASS","TITLE","CONTENT",

$arrBooks = array();

$fileDir = "../data/xiaohua//";
$arrFileList = $fileUtils->listFileName($fileDir);
foreach($arrFileList as $name){
	echo "file path : " . $name. "\n";
	
	$counter = 0;
	$row = 0;
	$handle = fopen($fileDir . $name, "r");
	while ($data = fgetcsv($handle)) {
		if($row == 0){// skip title
			$row++;
			continue;
		}
		
		$thisUrl 	= $data[0];
		$catName	= trim($data[1]);
		$title		= trim($data[2]);
		$content	= trim($data[3]);
		
		$content	= nl2br($content);		
		$content	= str_replace('<BR></BR>', "\n", $content);
		$content	= strip_tags($content);		
		
		
//		echo "catName : " . $catName. "\n";
//		echo "title : " . $title. "\n";
//		echo "content : " . $content. "\n";

		$sql = "SELECT id FROM category WHERE name = '" . addslashes($catName) . "'";
		
		$catId = $oCatImpl->getColumn(DB_TAG_CC, $sql);
		if(empty($catId)){
			$arrData = array(
				'name' 			=> $catName,
				'createdTime' 	=> "UTC_TIMESTAMP()",
				'status' 		=> "1",
				
				'descript' 		=> 'null',
			);
					
			$catId = $oCatImpl->insertArray(DB_TAG_CC, 'category', $arrData, " ON DUPLICATE KEY UPDATE modifiedTime=Values(modifiedTime)");
		}

		if($catId > 0){
			$sql = "SELECT id FROM content WHERE name = '" . addslashes($title) . "'";		
			$cId = $oCatImpl->getColumn(DB_TAG_CC, $sql);
			if(empty($cId)){			
				$arrData = array(
					'name' 			=> $title,
					'sorts' 		=> '0',
					'createdTime' 	=> "UTC_TIMESTAMP()",
					'status' 		=> "1",
					
					'content' 		=> $content,
				);

				$contid = $oCatImpl->insertArray(DB_TAG_CC, 'content', $arrData, " ON DUPLICATE KEY UPDATE modifiedTime=Values(modifiedTime)");
				
				
				$arrData = array(
					'categoryId' 	=> $catId,
					'contentId' 	=> $contid,
				);
						
				$oCatImpl->insertArray(DB_TAG_CC, 'content_category', $arrData, " ON DUPLICATE KEY UPDATE categoryId=Values(categoryId)");
			}
			
		}

//		$counter++;
//		
//		if($counter > 5)
//			break;		
	}
	
//	exit;
}


?>
